﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HML.Design;

namespace HML
{
    /// <summary>
    /// TabPagePlus(TabPage增强版)
    /// </summary>
    [Description("TabPagePlus(TabPage增强版)")]
    [DefaultProperty("Text")]
    [TypeConverter(typeof(PropertyOrderConverter))]
    [Designer(typeof(TabPagePlusDesigner))]
    [ToolboxItem(false)]
    public class TabPagePlus : Panel, ISkinObject
    {
        #region 主题

        private SkinObjectXmlMetadata skinObjectXmlMetadata = null;
        /// <summary>
        /// 主题对象在主题文件信息
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual SkinObjectXmlMetadata SkinObjectXmlMetadata
        {
            get
            {
                if (this.skinObjectXmlMetadata == null)
                    this.skinObjectXmlMetadata = new SkinObjectXmlMetadata(Assembly.GetAssembly(typeof(TabPagePlus)).GetName().Name, typeof(TabPagePlus).Name);

                return this.skinObjectXmlMetadata;
            }
        }

        /// <summary>
        /// 主题控制器
        /// </summary>
        [Description("主题控制器")]
        [Category("杂项")]
        [PropertyOrder(-360)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.All)]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual SkinController SkinController
        {
            get
            { return (this.Parent == null) ? null : ((ISkinObject)this.Parent).SkinController; }
            set { }
        }

        private SkinEnabledState skinEnabled = SkinEnabledState.Auto;
        /// <summary>
        /// 主题是否启用
        /// </summary>
        [Description("主题是否启用")]
        [Category("杂项")]
        [PropertyOrder(-300)]
        [DefaultValue(SkinEnabledState.Auto)]
        [RefreshProperties(RefreshProperties.All)]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual SkinEnabledState SkinEnabled
        {
            get { return this.skinEnabled; }
            set
            {
                if (this.skinEnabled == value)
                    return;

                this.skinEnabled = value;
                this.OnSkinChanged();
            }
        }

        private SkinStyle skinStyle = SkinStyle.Normal;
        /// <summary>
        /// 主题风格
        /// </summary>
        [Description("主题风格")]
        [Category("杂项")]
        [PropertyOrder(-260)]
        [DefaultValue(SkinStyle.Normal)]
        [RefreshProperties(RefreshProperties.All)]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual SkinStyle SkinStyle
        {
            get { return this.skinStyle; }
            set
            {
                if (this.skinStyle == value)
                    return;

                this.skinStyle = value;
                this.OnSkinChanged();
            }
        }

        /// <summary>
        /// 主题已更改
        /// </summary>
        public virtual void OnSkinChanged()
        {
            SkinManager.SyncSkinValueToProperty(this);
            this.Invalidate();
        }

        private ISkinObject followSkinObject = null;
        /// <summary>
        /// 主题设置跟随指定主题对象（自己的设置不再生效，提供给代码使用）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Localizable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual ISkinObject FollowSkinObject
        {
            get { return this.followSkinObject; }
            set
            {
                if (this.followSkinObject == value)
                    return;

                this.followSkinObject = value;
                this.OnSkinChanged();
            }
        }

        public virtual bool GetSkinObjectSkinStateCore()
        {
            return SkinManager.GetSkinObjectSkinState(this.Parent !=null?(ISkinObject)this.Parent: this);
        }

        void ISkinObject.InitializeInvalidate()
        {
            throw new NotImplementedException();
        }

        void ISkinObject.Invalidate()
        {
            this.Invalidate();
        }

        #endregion

        #region 停用事件

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler AutoSizeChanged
        {
            add { base.AutoSizeChanged += value; }
            remove { base.AutoSizeChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler DockChanged
        {
            add { base.DockChanged += value; }
            remove { base.DockChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler MarginChanged
        {
            add { base.MarginChanged += value; }
            remove { base.MarginChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler Resize
        {
            add { base.Resize += value; }
            remove { base.Resize -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler Move
        {
            add { base.Move += value; }
            remove { base.Move -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler LocationChanged
        {
            add { base.LocationChanged += value; }
            remove { base.LocationChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabIndexChanged
        {
            add { base.TabIndexChanged += value; }
            remove { base.TabIndexChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TabStopChanged
        {
            add { base.TabStopChanged += value; }
            remove { base.TabStopChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler FontChanged
        {
            add { base.FontChanged += value; }
            remove { base.FontChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackColorChanged
        {
            add { base.BackColorChanged += value; }
            remove { base.BackColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ForeColorChanged
        {
            add { base.ForeColorChanged += value; }
            remove { base.ForeColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler VisibleChanged
        {
            add { base.VisibleChanged += value; }
            remove { base.VisibleChanged -= value; }
        }

        #endregion

        #region 新增事件

        #region TabItem图标

        public delegate void TabItemIconDrawAfterEventHandler(object sender, TabControlPlusTabItemIconDrawAfterEventArgs e);
        private static readonly object EventTabItemIconDrawAfter = new object();
        /// <summary>
        /// TabItem图标绘制后事件
        /// </summary>
        [Description("TabItem图标绘制后事件")]
        [Category("杂项_图标")]
        public event TabItemIconDrawAfterEventHandler TabItemIconDrawAfter
        {
            add { Events.AddHandler(EventTabItemIconDrawAfter, value); }
            remove { Events.RemoveHandler(EventTabItemIconDrawAfter, value); }
        }

        #endregion

        #region TabItem关闭按钮

        public delegate void TabItemCloseButtonCloseingEventHandler(object sender, TabControlPlusTabItemCloseButtonCloseingEventArgs e);
        private static readonly object EventTabItemCloseButtonCloseing = new object();
        /// <summary>
        /// TabItem关闭时事件
        /// </summary>
        [Description("TabItem关闭时事件")]
        [Category("杂项_关闭按钮")]
        public event TabItemCloseButtonCloseingEventHandler TabItemCloseButtonCloseing
        {
            add { Events.AddHandler(EventTabItemCloseButtonCloseing, value); }
            remove { Events.RemoveHandler(EventTabItemCloseButtonCloseing, value); }
        }

        public delegate void TabCloseButtonClosedEventHandler(object sender, TabControlPlusTabItemCloseButtonClosedEventArgs e);
        private static readonly object EventTabItemCloseButtonClosed = new object();
        /// <summary>
        /// TabItem关闭后事件
        /// </summary>
        [Description("TabItem关闭后事件")]
        [Category("杂项_关闭按钮")]
        public event TabCloseButtonClosedEventHandler TabItemCloseButtonClosed
        {
            add { Events.AddHandler(EventTabItemCloseButtonClosed, value); }
            remove { Events.RemoveHandler(EventTabItemCloseButtonClosed, value); }
        }

        public delegate void TabItemCloseButtonDrawBeforeEventHandler(object sender, TabControlPlusTabItemCloseButtonDrawBeforeEventArgs e);
        private static readonly object EventTabItemCloseButtonDrawBefore = new object();
        /// <summary>
        /// TabItem关闭按钮绘制前事件
        /// </summary>
        [Description("TabItem关闭按钮绘制前事件")]
        [Category("杂项_关闭按钮")]
        public event TabItemCloseButtonDrawBeforeEventHandler TabItemCloseButtonDrawBefore
        {
            add { Events.AddHandler(EventTabItemCloseButtonDrawBefore, value); }
            remove { Events.RemoveHandler(EventTabItemCloseButtonDrawBefore, value); }
        }

        #endregion

        #endregion

        #region 新增属性

        private StyleAppearanceObject stateAppearance;
        /// <summary>
        /// 风格外观
        /// </summary>
        [Description("风格外观")]
        [Category("杂项")]
        [PropertyOrder(-200)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public StyleAppearanceObject StyleAppearance
        {
            get
            {
                if (this.stateAppearance == null)
                    this.stateAppearance = new StyleAppearanceObject(this, null);
                return this.stateAppearance;
            }
        }

        private TabItemObject tabItem;
        /// <summary>
        /// TabItem
        /// </summary>
        [Description("TabItem")]
        [Category("杂项")]
        [PropertyOrder(-182)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TabItemObject TabItem
        {
            get
            {
                if (this.tabItem == null)
                    this.tabItem = new TabItemObject(this);
                return this.tabItem;
            }
        }

        /// <summary>
        /// 鼠标是否已进入该选项
        /// </summary>
        [Description("鼠标是否已进入该选项")]
        [Browsable(false)]
        public bool IsMouseEnter
        {
            get
            {
                if (this.Parent == null || !(this.Parent is TabControlPlus))
                    return false;

                return ((TabControlPlus)this.Parent).IsMouseEnter(this);
            }
        }

        #region 用于临时存储运行信息

        /// <summary>
        /// 选项rect（包含:外边距、内边距、内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Rectangle M_Rect { get; set; }

        /// <summary>
        /// 选项rect（包含:内边距、内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Rectangle P_Rect { get; set; }

        /// <summary>
        /// 选项rect（只包含:内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Rectangle C_Rect { get; set; }

        /// <summary>
        /// 选项Size（只包含:内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Size Rect_C_Size { get; set; }

        /// <summary>
        /// 选项GraphicsPath(一般用选项的NoClipPaddingBounds范围生成)
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected GraphicsPath P_GP { get; set; }

        #region 图标

        /// <summary>
        /// 选项图标Size
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Size Icon_C_Size { get; set; }

        /// <summary>
        /// 选项图标rect(不包括内外边距)
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Rectangle Icon_C_Rect { get; set; }

        /// <summary>
        /// 选项图标rect(包括外边距)
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Rectangle Icon_M_Rect { get; set; }

        #endregion

        #region 文本

        /// <summary>
        /// 下拉面板选项文本Size
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Size Picker_Text_Size { get; set; }

        /// <summary>
        /// 选项文本Size
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected Size Text_C_Size { get; set; }

        /// <summary>
        /// 选项文本rect(排除外边距、边框、内边距)
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal Rectangle Text_C_Rect { get; set; }

        /// <summary>
        /// 文本的垂直布局字符串
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal protected string TextVerticalLayoutTmp { get; set; }

        #endregion

        #region 下拉面板

        private RectangleF dropDownPanelItem_Rect = RectangleF.Empty;
        /// <summary>
        /// 下拉面板选项Rect
        /// </summary>
        [Description("下拉面板选项Rect")]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public RectangleF DropDownPanelItem_Rect
        {
            get { return this.dropDownPanelItem_Rect; }
            set
            {
                if (this.dropDownPanelItem_Rect == value)
                    return;

                this.dropDownPanelItem_Rect = value;
            }
        }


        #endregion

        #endregion

        #endregion

        #region 重写属性

        [Browsable(true)]
        public override string Text
        {
            get { return base.Text; }
            set
            {
                if (base.Text == value)
                    return;

                base.Text = value;
                this.UpdatVerticalLayoutText();

                if (this.Parent != null && this.Parent is TabControlPlus && this.Parent.IsHandleCreated)
                {
                    TabControlPlus control = (TabControlPlus)this.Parent;
                    control.InitializeElement();
                    control.Invalidate();
                }
            }
        }

        #endregion

        #region 停用属性

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override AnchorStyles Anchor
        {
            get { return base.Anchor; }
            set { base.Anchor = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override bool AutoSize
        {
            get { return base.AutoSize; }
            set { base.AutoSize = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override AutoSizeMode AutoSizeMode
        {
            get { return base.AutoSizeMode; }
            set { base.AutoSizeMode = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override DockStyle Dock
        {
            get { return base.Dock; }
            set { base.Dock = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Point Location
        {
            get { return base.Location; }
            set { base.Location = value; }
        }

        [DefaultValue(typeof(Padding), "0,0,0,0")]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Padding Margin
        {
            get { return base.Margin; }
            set { base.Margin = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Size MaximumSize
        {
            get { return base.MaximumSize; }
            set { base.MaximumSize = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Size MinimumSize
        {
            get { return base.MaximumSize; }
            set { base.MaximumSize = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Size PreferredSize
        {
            get { return base.PreferredSize; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new bool Visible
        {
            get { return base.Visible; }
            set { base.Visible = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new int TabIndex
        {
            get { return base.TabIndex; }
            set { base.TabIndex = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new bool TabStop
        {
            get { return base.TabStop; }
            set { base.TabStop = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new BorderStyle BorderStyle
        {
            get { return base.BorderStyle; }
            set { base.BorderStyle = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override Font Font
        {
            get { return base.Font; }
            set { base.Font = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override RightToLeft RightToLeft
        {
            get { return base.RightToLeft; }
            set { base.RightToLeft = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Color BackColor
        {
            get { return this.StyleAppearance.BackColor; }
            set { }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Color ForeColor
        {
            get { return this.StyleAppearance.TextColor; }
            set { }
        }

        #endregion

        #region 构造

        public TabPagePlus()
        {
            this.SuspendLayout();
            this.Margin = new Padding(0);
            this.OnSkinChanged();
            this.ResumeLayout();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text">TabItem文本</param>
        public TabPagePlus(string text) : this()
        {
            this.SuspendLayout();
            this.Text = text;
            this.OnSkinChanged();
            this.ResumeLayout();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key">TabItem名称（可用于索引,必须唯一）</param>
        /// <param name="text">TabItem文本</param>
        public TabPagePlus(string key, string text) : this()
        {
            this.SuspendLayout();
            this.Name = key;
            this.Text = text;
            this.OnSkinChanged();
            this.ResumeLayout();
        }

        #endregion

        #region 重写

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            if (this.Parent != null && this.Parent is TabControlPlus && this.Parent.IsHandleCreated)
            {
                Rectangle display_rect = ((TabControlPlus)this.Parent).GetBodyDisplayRectangle();
                base.SetBoundsCore(display_rect.X, display_rect.Y, display_rect.Width, display_rect.Height, BoundsSpecified.All);
            }
            else
            {
                base.SetBoundsCore(x, y, width, height, specified);
            }
        }

        protected override ControlCollection CreateControlsInstance()
        {
            return new TabPagePlusControlsCollection(this);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.P_GP != null)
                {
                    this.P_GP.Dispose();
                }
                if (this.TabItem.Icon.Image != null)
                {
                    this.TabItem.Icon.Image = null;
                }
            }
            base.Dispose(disposing);
        }

        #endregion

        #region 虚方法

        //TabItem图标
        internal protected virtual void OnTabItemIconDrawAfter(TabControlPlusTabItemIconDrawAfterEventArgs e)
        {
            TabItemIconDrawAfterEventHandler handler = Events[EventTabItemIconDrawAfter] as TabItemIconDrawAfterEventHandler;
            if (handler != null && this.Parent != null && this.Parent is TabControlPlus)
            {
                handler(this.Parent, e);
            }
        }

        //TabItem关闭按钮
        internal protected virtual void OnTabItemCloseButtonCloseing(TabControlPlusTabItemCloseButtonCloseingEventArgs e)
        {
            TabItemCloseButtonCloseingEventHandler handler = Events[EventTabItemCloseButtonCloseing] as TabItemCloseButtonCloseingEventHandler;
            if (handler != null && this.Parent != null && this.Parent is TabControlPlus)
            {
                handler(this.Parent, e);
            }
        }

        internal protected virtual void OnTabItemCloseButtonClosed(TabControlPlusTabItemCloseButtonClosedEventArgs e)
        {
            TabCloseButtonClosedEventHandler handler = Events[EventTabItemCloseButtonClosed] as TabCloseButtonClosedEventHandler;
            if (handler != null && this.Parent != null && this.Parent is TabControlPlus)
            {
                handler(this.Parent, e);
            }
        }

        internal protected virtual void OnTabItemCloseButtonDrawBefore(TabControlPlusTabItemCloseButtonDrawBeforeEventArgs e)
        {
            TabItemCloseButtonDrawBeforeEventHandler handler = Events[EventTabItemCloseButtonDrawBefore] as TabItemCloseButtonDrawBeforeEventHandler;
            if (handler != null && this.Parent != null && this.Parent is TabControlPlus)
            {
                handler(this.Parent, e);
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 更新垂直方向文本
        /// </summary>
        private void UpdatVerticalLayoutText()
        {
            string text_tmp = "";
            for (int j = 0; j < this.Text.Length; j++)
            {
                text_tmp += this.Text[j];
                if (j < this.Text.Length - 1)
                {
                    text_tmp += Environment.NewLine;
                }
            }
            this.TextVerticalLayoutTmp = text_tmp;

            if (this.Parent != null && this.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.Parent;
                tabControl.InitializeElement();
                tabControl.Invalidate();
            }
        }

        #endregion

        #region 类

        /// <summary>
        /// 重写TabPagePlus子控件集合
        /// </summary>
        [ComVisible(false)]
        public class TabPagePlusControlsCollection : Control.ControlCollection
        {
            public TabPagePlusControlsCollection(TabPagePlus owner) : base(owner) { }

            public override void Add(Control value)
            {
                if (value is TabPagePlus)
                {
                    throw new ArgumentException("TabPagePlus的子控件不能为TabPagePlus类型");
                }

                base.Add(value);
            }
        }

        [TypeConverter(typeof(PropertyOrderConverter))]
        public class TabItemObject
        {
            private TabPagePlus owner = null;
            internal TabItemObject(TabPagePlus owner) { this.owner = owner; }

            #region 属性

            private TabControlPlusTabItemAutoWidthMode autoWidth = TabControlPlusTabItemAutoWidthMode.Auto;
            /// <summary>
            /// TabItem是否为自动宽度
            /// </summary>
            [Description("TabItem是否为自动宽度")]
            [Category("杂项")]
            [PropertyOrder(-198)]
            [DefaultValue(TabControlPlusTabItemAutoWidthMode.Auto)]
            public TabControlPlusTabItemAutoWidthMode AutoWidth
            {
                get { return this.autoWidth; }
                set
                {
                    if (this.autoWidth == value)
                        return;

                    this.autoWidth = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.InitializeElement();
                        tabControl.Invalidate();
                    }
                }
            }

            private int autoWidthMin = -1;
            /// <summary>
            /// TabItem自动宽度最小宽度(-1表示采用通用设置)
            /// </summary>
            [Description("TabItem自动宽度最小宽度(-1表示采用通用设置)")]
            [Category("杂项")]
            [PropertyOrder(-196)]
            [DefaultValue(-1)]
            public int AutoWidthMin
            {
                get { return this.autoWidthMin; }
                set
                {
                    if (this.autoWidthMin == value || value < -1)
                        return;

                    this.autoWidthMin = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.InitializeElement();
                        tabControl.Invalidate();
                    }
                }
            }

            private int fixedWidth = -1;
            /// <summary>
            /// TabItem固定宽度(-1表示采用通用设置)
            /// </summary>
            [Description("TabItem固定宽度(-1表示采用通用设置)")]
            [Category("杂项")]
            [PropertyOrder(-194)]
            [DefaultValue(-1)]
            public int FixedWidth
            {
                get { return this.fixedWidth; }
                set
                {
                    if (this.fixedWidth == value || value < -1)
                        return;

                    this.fixedWidth = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.InitializeElement();
                        tabControl.Invalidate();
                    }
                }
            }

            private LRMargin lRMargin = new LRMargin(-1, -1);
            /// <summary>
            /// TabItem左右外边距(-1, -1表示采用通用设置)
            /// </summary>
            [Description("TabItem左右外边距(-1, -1表示采用通用设置)")]
            [Category("杂项")]
            [PropertyOrder(-192)]
            [DefaultValue(typeof(LRMargin), "-1, -1")]
            public LRMargin LRMargin
            {
                get { return this.lRMargin; }
                set
                {
                    if (this.lRMargin == value || value.Left < -1 || value.Right < -1)
                        return;

                    this.lRMargin = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.InitializeElement();
                        tabControl.Invalidate();
                    }
                }
            }

            private LRPadding lRPadding = new LRPadding(-1, -1);
            /// <summary>
            /// TabItem左右内边距(-1, -1表示采用通用设置)
            /// </summary>
            [Description("TabItem左右内边距(-1, -1表示采用通用设置)")]
            [Category("杂项")]
            [PropertyOrder(-190)]
            [DefaultValue(typeof(LRPadding), "-1, -1")]
            public LRPadding LRPadding
            {
                get { return this.lRPadding; }
                set
                {
                    if (this.lRPadding == value || value.Left < -1 || value.Right < -1)
                        return;

                    this.lRPadding = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.InitializeElement();
                        tabControl.Invalidate();
                    }
                }
            }

            private bool verticalLayout = false;
            /// <summary>
            /// TabItem文本是否垂直布局(限于左右两边)
            /// </summary>
            [Description("TabItem文本是否垂直布局(限于左右两边)")]
            [Category("杂项")]
            [PropertyOrder(-188)]
            [DefaultValue(false)]
            public bool VerticalLayout
            {
                get { return this.verticalLayout; }
                set
                {
                    if (this.verticalLayout == value)
                        return;

                    this.verticalLayout = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        if (tabControl.TabBar.Alignment == TabControlPlusTabBarAlignment.Left || tabControl.TabBar.Alignment == TabControlPlusTabBarAlignment.Right)
                        {
                            tabControl.InitializeElement();
                            tabControl.Invalidate();
                        }
                    }
                }
            }

            private bool textEllipsisCharacter = false;
            /// <summary>
            /// TabItem文本超出范围是否使用省略号代替
            /// </summary>
            [Description("TabItem文本超出范围是否使用省略号代替")]
            [Category("杂项")]
            [PropertyOrder(-186)]
            [DefaultValue(false)]
            public bool TextEllipsisCharacter
            {
                get { return this.textEllipsisCharacter; }
                set
                {
                    if (this.textEllipsisCharacter == value)
                        return;

                    this.textEllipsisCharacter = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.Invalidate();
                    }
                }
            }

            private bool enabled = true;
            /// <summary>
            /// TabItem启用状态
            /// </summary>
            [Description("TabItem启用状态")]
            [Category("杂项")]
            [PropertyOrder(-184)]
            [DefaultValue(true)]
            public bool Enabled
            {
                get { return this.enabled; }
                set
                {
                    if (this.enabled == value)
                        return;

                    this.enabled = value;
                    if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                    {
                        TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                        tabControl.OnTabItemEnabledChanged(new TabControlPlusTabItemOperatedEventArgs(this.owner));
                        tabControl.Invalidate();
                    }
                }
            }

            private IconObject icon;
            /// <summary>
            /// 图标
            /// </summary>
            [Description("图标")]
            [Category("杂项")]
            [PropertyOrder(-182)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public IconObject Icon
            {
                get
                {
                    if (this.icon == null)
                        this.icon = new IconObject(this.owner);
                    return this.icon;
                }
            }

            private TabControlPlusTabItemCustomButtonCollection customButtonCollection;
            /// <summary>
            /// TabItem自定义按钮集合
            /// </summary>
            [Description("TabItem自定义按钮集合")]
            [Category("杂项")]
            [PropertyOrder(-180)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public TabControlPlusTabItemCustomButtonCollection CustomButtons
            {
                get
                {
                    if (this.customButtonCollection == null)
                        this.customButtonCollection = new TabControlPlusTabItemCustomButtonCollection(this.owner);
                    return this.customButtonCollection;
                }
            }

            private CloseButtonObject closeButton;
            /// <summary>
            /// TabItem关闭按钮
            /// </summary>
            [Description("TabItem关闭按钮")]
            [Category("杂项")]
            [PropertyOrder(-178)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public CloseButtonObject CloseButton
            {
                get
                {
                    if (this.closeButton == null)
                        this.closeButton = new CloseButtonObject(this.owner);
                    return this.closeButton;
                }
            }

            #endregion

            [TypeConverter(typeof(PropertyOrderConverter))]
            public class IconObject
            {
                private TabPagePlus owner = null;
                internal IconObject(TabPagePlus owner) { this.owner = owner; }

                #region 属性

                private TabControlPlusTabItemIconVisibles visible = TabControlPlusTabItemIconVisibles.Auto;
                /// <summary>
                /// 是否显示图标
                /// </summary>
                [Description("是否显示图标")]
                [PropertyOrder(-170)]
                [DefaultValue(TabControlPlusTabItemIconVisibles.Auto)]
                public TabControlPlusTabItemIconVisibles Visible
                {
                    get { return this.visible; }
                    set
                    {
                        if (this.visible == value)
                            return;

                        this.visible = value;
                        if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                        {
                            TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                            tabControl.InitializeElement();
                            tabControl.Invalidate();
                        }
                    }
                }

                private Image image = null;
                /// <summary>
                /// 图标
                /// </summary>
                [Description("图标")]
                [PropertyOrder(-168)]
                [DefaultValue(null)]
                [RefreshProperties(RefreshProperties.Repaint)]
                public Image Image
                {
                    get { return this.image; }
                    set
                    {
                        if (this.image == value)
                            return;

                        this.image = value;
                        if (this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                        {
                            TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                            tabControl.Invalidate();
                        }
                    }
                }

                #endregion

            }

            [TypeConverter(typeof(PropertyOrderConverter))]
            public class CloseButtonObject
            {
                private TabPagePlus owner = null;
                internal CloseButtonObject(TabPagePlus owner) { this.owner = owner; }

                #region 属性

                private TabControlPlusTabItemCloseButtonVisibles visible = TabControlPlusTabItemCloseButtonVisibles.Auto;
                /// <summary>
                /// TabItem关闭按钮是否显示(Auto表示采用控件通用设置)
                /// </summary>
                [Description("TabItem关闭按钮是否显示(Auto表示采用控件通用设置)")]
                [PropertyOrder(-118)]
                [DefaultValue(TabControlPlusTabItemCloseButtonVisibles.Auto)]
                public TabControlPlusTabItemCloseButtonVisibles Visible
                {
                    get { return this.visible; }
                    set
                    {
                        if (this.visible == value)
                            return;

                        this.visible = value;
                        if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                        {
                            TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                            tabControl.InitializeElement();
                            tabControl.Invalidate();
                        }
                    }
                }

                private bool enabled = true;
                /// <summary>
                /// TabItem使用状态是否已启动
                /// </summary>
                [Description("TabItem使用状态是否已启动")]
                [PropertyOrder(-116)]
                [DefaultValue(true)]
                public bool Enabled
                {
                    get { return this.enabled; }
                    set
                    {
                        if (this.enabled == value)
                            return;

                        this.enabled = value;
                        if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
                        {
                            TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                            tabControl.Invalidate();
                        }
                    }
                }

                /// <summary>
                /// 选项关闭按钮rect(包括外边距)
                /// </summary>
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                internal Rectangle M_Rect { get; set; }

                /// <summary>
                /// 选项关闭按钮rect(不包括内外边距)
                /// </summary>
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                internal Rectangle C_Rect { get; set; }

                #endregion

            }

        }

        #endregion

        #region 外观

        public class StyleAppearanceObject : AppearanceObjectBase
        {
            internal StyleAppearanceObject(ISkinObject owner, AppearanceObjectBase parent) : base(owner, parent) { }

            #region 属性

            private Color skinBackColor = Color.Empty;
            private readonly Color defaultBackColor = SystemColors.Control;
            private Color backColor = Color.Empty;
            /// <summary>
            /// 背景颜色
            /// </summary>
            [Description("背景颜色")]
            [PropertyOrder(-195)]
            [SkinProperty(true, true)]
            public Color BackColor
            {
                get
                {
                    if (this.owner.GetSkinObjectSkinStateCore() && this.skinBackColor != Color.Empty)
                    {
                        return this.skinBackColor;
                    }

                    if (this.backColor != Color.Empty)
                    {
                        return this.backColor;
                    }

                    if (((Control)this.owner).Parent != null)
                    {
                        return ((Control)this.owner).Parent.BackColor;
                    }

                    return this.defaultBackColor;
                }
                set
                {
                    if (this.backColor == value)
                        return;

                    this.backColor = value;
                    this.Invalidate();
                }
            }
            private bool ShouldSerializeBackColor()
            {
                return this.backColor != Color.Empty;
            }
            private void ResetBackColor()
            {
                this.backColor = Color.Empty;
                this.Invalidate();
            }

            private Color skinTextColor = Color.Empty;
            private readonly Color defaultTextColor = SystemColors.ControlText;
            private Color textColor = Color.Empty;
            /// <summary>
            /// 文本颜色
            /// </summary>
            [Description("文本颜色")]
            [PropertyOrder(-193)]
            [SkinProperty(true, true)]
            public Color TextColor
            {
                get
                {
                    if (this.owner.GetSkinObjectSkinStateCore() && this.skinTextColor != Color.Empty)
                    {
                        return this.skinTextColor;
                    }

                    if (this.textColor != Color.Empty)
                    {
                        return this.textColor;
                    }

                    if (((Control)this.owner).Parent != null)
                    {
                        return ((Control)this.owner).Parent.ForeColor;
                    }

                    return this.defaultTextColor;
                }
                set
                {
                    if (this.textColor == value)
                        return;

                    this.textColor = value;
                    this.Invalidate();
                }
            }
            private bool ShouldSerializeTextColor()
            {
                return this.textColor != Color.Empty;
            }
            private void ResetTextColor()
            {
                this.textColor = Color.Empty;
                this.Invalidate();
            }

            #endregion

        }

        #endregion

    }

}
